package com.example.backend.controller;

import com.example.backend.domain.entity.User;
import com.example.backend.dto.ApiResponse;
import com.example.backend.service.AuthService;
import com.example.backend.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 仪表板控制器
 * 根据用户角色提供不同的仪表板数据
 */
@RestController
@RequestMapping("/api/dashboard")
@Tag(name = "仪表板", description = "用户仪表板数据API")
@SecurityRequirement(name = "Bearer Authentication")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private AuthService authService;

    @GetMapping("/overview")
    @Operation(summary = "获取仪表板概览", description = "根据用户角色获取相应的仪表板概览数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getDashboardOverview(HttpServletRequest request) {
        try {
            String token = extractTokenFromRequest(request);
            User currentUser = authService.validateToken(token);
            
            Map<String, Object> overview = dashboardService.getDashboardOverview(currentUser);
            return ResponseEntity.ok(ApiResponse.success("获取仪表板概览成功", overview));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取仪表板概览失败: " + e.getMessage()));
        }
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取仪表板统计", description = "根据用户角色获取相应的统计数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getDashboardStatistics(HttpServletRequest request) {
        try {
            String token = extractTokenFromRequest(request);
            User currentUser = authService.validateToken(token);
            
            Map<String, Object> statistics = dashboardService.getDashboardStatistics(currentUser);
            return ResponseEntity.ok(ApiResponse.success("获取仪表板统计成功", statistics));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取仪表板统计失败: " + e.getMessage()));
        }
    }

    @GetMapping("/recent-activities")
    @Operation(summary = "获取最近活动", description = "根据用户角色获取相应的最近活动")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getRecentActivities(HttpServletRequest request) {
        try {
            String token = extractTokenFromRequest(request);
            User currentUser = authService.validateToken(token);
            
            Map<String, Object> activities = dashboardService.getRecentActivities(currentUser);
            return ResponseEntity.ok(ApiResponse.success("获取最近活动成功", activities));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取最近活动失败: " + e.getMessage()));
        }
    }

    @GetMapping("/charts/ticket-trend")
    @Operation(summary = "获取工单趋势图表", description = "根据用户角色获取相应的工单趋势图表数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTicketTrendChart(HttpServletRequest request) {
        try {
            String token = extractTokenFromRequest(request);
            User currentUser = authService.validateToken(token);
            
            Map<String, Object> chartData = dashboardService.getTicketTrendChart(currentUser);
            return ResponseEntity.ok(ApiResponse.success("获取工单趋势图表成功", chartData));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取工单趋势图表失败: " + e.getMessage()));
        }
    }

    @GetMapping("/charts/ticket-status")
    @Operation(summary = "获取工单状态图表", description = "根据用户角色获取相应的工单状态分布图表数据")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTicketStatusChart(HttpServletRequest request) {
        try {
            String token = extractTokenFromRequest(request);
            User currentUser = authService.validateToken(token);
            
            Map<String, Object> chartData = dashboardService.getTicketStatusChart(currentUser);
            return ResponseEntity.ok(ApiResponse.success("获取工单状态图表成功", chartData));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error("获取工单状态图表失败: " + e.getMessage()));
        }
    }

    private String extractTokenFromRequest(HttpServletRequest request) {
        String bearerToken = request.getHeader("Authorization");
        if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
            return bearerToken.substring(7);
        }
        throw new RuntimeException("无效的认证令牌");
    }
}
